const app = getApp()
const timeChange = require('../../utils/timeChange.js')
const toast = require('../../utils/toast.js')
var phonetel = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startTime: '',
    endTime: '',
    defaultNums: 1,
    checkNums: 1, //当前选择套数
    nums: 1, //可以选择的总房间套数
    personNums: 1, //当前选择入住人数
    persons: 1, //可以选择入住总人数
    isSelected: true,
    isShowTip: false,
    isFinish: false, //是否关闭订单填写页，
    checkDays: '共*天',
    checkInDate: '*月*日',
    checkOutDate: '*月*日', 
    isShow: false, //是否显示toast弹窗
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getIntentData(options)
    this.connectNetForData()
  },

  connectNetForData() {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })

    console.log(app.baseNet + 'roomInfo/queryReservationRoomInfo?sriId=' + that.data.sriId + '&username=' + that.data.username + '&flag=' + 1)

    wx.request({
      url: app.baseNet + 'roomInfo/queryReservationRoomInfo',
      data: {
        sriId: that.data.sriId,
        username: that.data.username,
        flag: 1,
        startTime: that.data.startTime,
        endTime: that.data.endTime,
        defaultNums: that.data.defaultNums
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.statusCode == 200) {
          var orderData = res.data
          console.log('**********' + orderData)
          if (orderData.messcode == '001') {
            var order = orderData.reservationRoomDto
            var overnight = order.reservationRoom.whetherOvernight
            var checkDays = 1;
            if (overnight == 1) {
              checkDays = timeChange.dateTimeDifferenceOvernight(order.startTime, order.endTime)
            } else {
              checkDays = timeChange.dateTimeDifferenceNotOvernight(order.startTime, order.endTime)
            }
            var acturalPrice = order.totalPrice
            if (order.price != null) {
              acturalPrice = order.totalPrice - order.price
            }

            console.log('::::::::' + order + ':::::::::::' + order.endTime)
            that.setData({
              reservationRoomDto: order,
              reservationRoom: order.reservationRoom,
              startTime: order.startTime,
              checkInDate: order != null && order.startTime != null && typeof order.startTime != 'undefined' ? timeChange.dateToString(order.startTime) : '*月*日',
              checkInTime: order.reservationRoom.checkTime,
              checkDays: '共' + checkDays + '天',
              endTime: order.endTime,
              checkOutDate: order != null && order.endTime != null && typeof order.endTime != 'undefined' ? timeChange.dateToString(order.endTime) : '*月*日',
              checkOutTime: order.reservationRoom.checkOutTime,
              checkNums: order.defaultNums,
              nums: order.nums,
              personNums: order.defaultCheckNumber,
              persons: order.checkNumber,
              totalPrice: '￥' + order.totalPrice,
              totalSinglePrice: order.totalPrice,
              totalPP: order.totalPrice,
              couponPrice: order.price,
              acturalPrice: acturalPrice,
              overnight: overnight,
              scId: order.scId,
              isShowTip: false
            })
            wx.hideLoading()
          }
        }

      }
    })
  },

  /**
   * 增加预定房源套数
   */
  addRoom: function(e) {
    var that = this
    if (this.data.checkNums < this.data.nums) { //当前预定的房源套数小于可预订的最大套数，可以增加预定套数
      var bookRoom = this.data.checkNums + 1
      this.setData({
        checkNums: bookRoom,
        totalPP: bookRoom * that.data.totalSinglePrice,
        totalPrice: '￥' + bookRoom * that.data.totalSinglePrice,
        acturalPrice: that.data.couponPrice != null ? bookRoom * that.data.totalSinglePrice - that.data.couponPrice : bookRoom * that.data.totalSinglePrice
      })
    }
  },

  /**
   * 减少预定房源套数
   */
  reduceRoom: function(e) {
    var that = this
    if (this.data.checkNums > 1) {
      var bookRoom = this.data.checkNums - 1
      this.setData({
        checkNums: bookRoom,
        totalPP: bookRoom * that.data.totalSinglePrice,
        totalPrice: '￥' + bookRoom * that.data.totalSinglePrice,
        acturalPrice: that.data.couponPrice != null ? bookRoom * that.data.totalSinglePrice - that.data.couponPrice : bookRoom * that.data.totalSinglePrice
      })
    }
  },

  /**
   *增加入住人数 
   */
  addPerson: function(e) {
    if (this.data.personNums < this.data.persons) { //当前入住人数小于可入住最大人数，可以增加入住人数
      var checkPerson = this.data.personNums + 1;
      this.setData({
        personNums: checkPerson
      })
    }
  },

  /**
   * 减少入住人数
   */
  reducePerson: function(e) {
    if (this.data.personNums > 1) {
      var checkPerson = this.data.personNums - 1
      this.setData({
        personNums: checkPerson
      })
    }
  },

  /**
   * 传递参数
   */
  getIntentData(options) {
    var roomId = options.roomId
    var username = options.username
    this.setData({
      sriId: roomId,
      username: username,
      phone: username,
      isFinish: false
    })
  },

  /**
   *是否同意山海边服务协议 
   */
  checkProtocol: function(e) {
    var sel = !this.data.isSelected
    this.setData({
      isSelected: sel
    })
  },

  /**
   * 山海边服务协议
   */
  toShbProtocol: function(e) {
    wx.navigateTo({
      url: '../protocol/protocol',
    })
  },

  /**
   * 日期选择 
   */
  selectDate: function(e) {
    var that = this
    wx.navigateTo({
      url: '../calendar/calendar?isReserve=' + 1 + '&roomId=' + that.data.sriId + '&startTime=' + that.data.startTime + '&endTime=' + that.data.endTime + '&whetherOvernight=' + that.data.overnight,
    })
  },

  /**
   * 查看明细
   */
  checkPriceDetails: function(e) {
    var that = this
    wx.navigateTo({
      url: '../order_searchDetail/order_searchDetail?sriRid=' + that.data.sriId + '&startTime=' + that.data.startTime + '&endTime=' + that.data.endTime + '&reserveNumber=' + that.data.checkNums,
    })
  },

  /**
   * 优惠劵选择
   */
  couponSelect: function(e) {
    var that = this
    wx.navigateTo({
      url: '../order_coupons/order_coupons?priceTotal=' + that.data.totalPP + '&username=' + that.data.username + '&scId=' + that.data.scId,
    })
  },
  /**
   * 输入预定人监听
   */
  bookInput: function(e) {
    var input = e.detail.value;
    console.log('bookInput: ' + input)
    this.setData({
      checkName: input
    })
  },

  /**
   *预定电话输入监听
   */
  phoneNumberInput: function(e) {
    var input = e.detail.value;
    console.log('bookInput: ' + input)
    this.setData({
      phone: input
    })
  },


  /**
   * 预定房源
   */
  reserveRoom: function(e) {
    if (this.data.isSelected) {
      if (this.data.checkName == null || this.data.checkName == '' || this.data.checkName == 'undefined') {
        toast.toastShow(this, '请输入预定人')
      } else if (this.data.phone == null || this.data.phone == '' || this.data.phone == 'undefined') {
        toast.toastShow(this, '手机号不能为空')
      } else if (this.data.phone.length < 11) {
        toast.toastShow(this, '手机号长度有误！')
      } else if (!phonetel.test(this.data.phone)) {
        toast.toastShow(this, '请输入正确的手机号')
      } else {
        this.bookRoomSubmit()
      }
    }
  },
  /**
   * 预定房源提交
   */
  bookRoomSubmit() {
    var that = this
    wx.showLoading({
      title: '加载中...',
    })

    var ss = that.data.scId == null ? '' : that.data.scId
    var dd = that.data.couponPrice == null ? 0 : that.data.couponPrice
    console.log(app.baseNet + 'orderInfo/insertOrderInfo?userName=' + that.data.username + '&sriRid=' + that.data.sriId + '&startTime=' + that.data.startTime + '&endTime=' + that.data.endTime + '&name=' + that.data.checkName + '&priceTotal=' + that.data.totalPP + '&phone=' + that.data.phone + '&reserveNumber=' + that.data.checkNums + '&checkNumber=' + that.data.personNums + '&couponPrice=' + dd + '&actualPrice=' + that.data.acturalPrice + '&scRid=' + ss)
    wx.request({
      url: app.baseNet + 'orderInfo/insertOrderInfo',
      data: {
        userName: that.data.username,
        sriRid: that.data.sriId,
        startTime: that.data.startTime,
        endTime: that.data.endTime,
        name: that.data.checkName,
        phone: that.data.phone,
        reserveNumber: that.data.checkNums,
        checkNumber: that.data.personNums,
        priceTotal: that.data.totalPP,
        couponPrice: that.data.couponPrice == null ? 0 : that.data.couponPrice,
        actualPrice: that.data.acturalPrice,
        scRid: that.data.scId == null ? '' : that.data.scId
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(res) {
        if (res.statusCode == 200) {
          var reserveResult = res.data
          wx.hideLoading()
          if (reserveResult.messcode == '001') {
            wx.navigateTo({
              url: '../reserve_success/reserve_success?soiId=' + reserveResult.orderInfoDto.soiId + '&roomTitle=' + that.data.reservationRoomDto.reservationRoom.roomTitle + '&date=' + (that.data.checkInDate + '至' + that.data.checkOutDate + '   ' + that.data.checkDays + '   ' + that.data.checkNums + '套' + '&sriId=' + that.data.sriId),
            })

            that.setData({
              isFinish: true
            })
          }

        } else {
          wx.hideLoading()
          wx.showToast({
            title: '系统开小差了~',
          })
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    if (this.data.isShowTip) {
      wx.showLoading({
        title: '加载中...',
      })
    }
    if (this.data.isFinish) { //结束界面
      var pages = getCurrentPages()
      var curPage = pages[pages.length - 1]
      var prePage = pages[pages.length - 2]
      prePage.setData({
        sriRid: that.data.sriId,
        username: that.data.username,
        isDess: true
      })
      prePage.connectNetForData()
      wx.navigateBack({
        delta: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})